#include "hardware.h"

const uint8_t maxcount = 10;
static uint8_t countts,countnl = 0;

static uint8_t count[17] = {0};


void scankey(int8_t keylist[])
{
    uint8_t p = 0;
    if(!LL_GPIO_IsInputPinSet(num0_GPIO_Port,num0_Pin))
    {
        if(count[0] < maxcount)
            count[0]++;
        else
        {
            keylist[p] = 0x62;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[0] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(num1_GPIO_Port,num1_Pin))
    {
        if(count[1] < maxcount)
            count[1]++;
        else
        {
            keylist[p] = 0x59;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[1] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(num2_GPIO_Port,num2_Pin))
    {
        if(count[2] < maxcount)
            count[2]++;
        else
        {
            keylist[p] = 0x5A;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[2] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(num3_GPIO_Port,num3_Pin))
    {
        if(count[3] < maxcount)
            count[3]++;
        else
        {
            keylist[p] = 0x5B;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[3] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(num4_GPIO_Port,num4_Pin))
    {
        if(count[4] < maxcount)
            count[4]++;
        else
        {
            keylist[p] = 0x5C;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[4] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(num5_GPIO_Port,num5_Pin))
    {
        if(count[5] < maxcount)
            count[5]++;
        else
        {
            keylist[p] = 0x5D;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[5] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(num6_GPIO_Port,num6_Pin))
    {
        if(count[6] < maxcount)
            count[6]++;
        else
        {
            keylist[p] = 0x5E;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[6] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(num7_GPIO_Port,num7_Pin))
    {
        if(count[7] < maxcount)
            count[7]++;
        else
        {
            keylist[p] = 0x5F;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[7] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(num8_GPIO_Port,num8_Pin))
    {
        if(count[8] < maxcount)
            count[8]++;
        else
        {
            keylist[p] = 0x60;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[8] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(num9_GPIO_Port,num9_Pin))
    {
        if(count[9] < maxcount)
            count[9]++;
        else
        {
            keylist[p] = 0x61;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[9] = 0;
        keylist[p] = 0;
    }


    //符号
    if(!LL_GPIO_IsInputPinSet(dot_GPIO_Port,dot_Pin))
    {
        if(count[10] < maxcount)
            count[10]++;
        else
        {
            keylist[p] = 0x63;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[10] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(NumLock_GPIO_Port,NumLock_Pin))
    {
        if(count[11] < maxcount)
            count[11]++;
        else
        {
            keylist[p] = 0x53;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[11] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(chu_GPIO_Port,chu_Pin))
    {
        if(count[12] < maxcount)
            count[12]++;
        else
        {
            keylist[p] = 0x54;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[12] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(cheng_GPIO_Port,cheng_Pin))
    {
        if(count[13] < maxcount)
            count[13]++;
        else
        {
            keylist[p] = 0x55;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[13] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(jian_GPIO_Port,jian_Pin))
    {
        if(count[14] < maxcount)
            count[14]++;
        else
        {
            keylist[p] = 0x56;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[14] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(jia_GPIO_Port,jia_Pin))
    {
        if(count[15] < maxcount)
            count[15]++;
        else
        {
            keylist[p] = 0x57;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[15] = 0;
        keylist[p] = 0;
    }

    if(!LL_GPIO_IsInputPinSet(Enter_GPIO_Port,Enter_Pin))
    {
        if(count[16] < maxcount)
            count[16]++;
        else
        {
            keylist[p] = 0x58;
            if(p == 5)return;
            p++;
        }
    }
    else
    {
        count[16] = 0;
        keylist[p] = 0;
    }
}

void scan_ts(int8_t *num) //num6
{
    if(!LL_GPIO_IsInputPinSet(num6_GPIO_Port,num6_Pin))
    {
        countts++;
        if(countts == maxcount)
            *num = 0x5E;
    }
    else
    {
        countts = 0;
        *num = 0;
    }
}

void scan_tsnumlock(int8_t *nl) //numlock
{
    if(!LL_GPIO_IsInputPinSet(NumLock_GPIO_Port,NumLock_Pin))
    {
        countnl++;
        if(countnl == maxcount)
        {
            *nl = 0x53;
            // LED_TOGGLE();
        }
    }
    else
    {
        countnl = 0;
        *nl = 0;
    }
}

